package cn.virens.common.boot.provider.apijson;

import apijson.orm.AbstractSQLExecutor;
import apijson.orm.SQLConfig;
import cn.virens.common.util.core.Assert;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import javax.sql.DataSource;
import java.sql.Connection;

public class APIJSONSQLExecutor extends AbstractSQLExecutor<String, JSONObject, JSONArray> {
    private final DataSource mDataSource;

    public APIJSONSQLExecutor(APIJSONParser parser) {
        this.mDataSource = parser.dataSource;
    }

    @Override
    public Connection getConnection(SQLConfig<String, JSONObject, JSONArray> sconfig) throws Exception {
        String k = sconfig.getDatasource() + "-" + sconfig.getDatabase();
        Connection sqlConnection = connectionMap.get(Assert.isEmpty(k));
        if (sqlConnection == null || sqlConnection.isClosed()) {
            connectionMap.put(k, mDataSource.getConnection());
        }

        return super.getConnection(sconfig);
    }
}
